package com.shop.service.impl;

import com.shop.dao.ContractDao;
import com.shop.domain.Contract;
import com.shop.domain.Page;
import com.shop.service.ContractService;
import com.shop.util.IDUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ContractServiceImpl
 * Created by heqianqian on 2017/7/11.
 */
@Service
public class ContractServiceImpl implements ContractService<Contract> {

    @Resource
    private ContractDao contractDao;

    @Override
    public List findPage(Page page) {
        return contractDao.findPage(page);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Contract> find(Map paraMap) {
        return contractDao.find(paraMap);
    }

    @Override
    public Contract get(Serializable id) {
        return (Contract) contractDao.get(id);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void insert(Contract contract) {
        //设置ID
        contract.setContractId(IDUtils.getId());
        //设置状态 默认草稿状态
        contract.setState(0);
        contractDao.insert(contract);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void update(Contract contract) {
        contractDao.update(contract);
    }

    @Override
    public void deleteById(Serializable id) {
        Serializable[] ids = {id};
        //首先删除所有的货物
        try {
            contractDao.deleteContractProductById(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //再删除合同
        contractDao.deleteById(id);
    }

    @Override
    public void delete(Serializable[] ids) {
        //首先删除所有的货物
        try {
            contractDao.deleteContractProductById(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //再删除合同
        contractDao.delete(ids);
    }

    @Override
    public void submit(Serializable[] ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("state", 1);
        contractDao.updateState(map);
    }

    @Override
    public void cancel(Serializable[] ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("state", 0);
        contractDao.updateState(map);
    }
}
